package com.boxing;

import com.boxing.utils.Utils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.Map;

// java代码中 路径中的  /  代表 ip:8080/webDemo
@WebServlet(name = "MyServlet123", urlPatterns = "/myServlet", initParams = {@WebInitParam(name = "enCode", value = "utf-8")})
public class MyServlet extends HttpServlet {
    private static Logger LOG = LogManager.getLogger(MyServlet.class);
    String enCode;
    private ServletContext servletContext;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        System.out.println("MyServlet init 执行了");
        // 获取配置的init-params参数
        enCode = config.getInitParameter("enCode");
        // 获取servlet的别名
        String servletName = config.getServletName();
        System.out.println("servletConfig params:"+ enCode +"   servlet别名："+ servletName);
        servletContext = config.getServletContext();
        String value = servletContext.getInitParameter("key");
        String value2 = servletContext.getInitParameter("key2");
        System.out.println("ServletContext 的配置：key:" + value +"  key2:"+value2);
        // 获取工程的绝对路径
        String realPath = servletContext.getRealPath("/");
        // 获取工程名称
        String contextPath1 = servletContext.getContextPath();
        System.out.println("工程的绝对路径:"+realPath +"   工程名称:" + contextPath1);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(enCode);
        resp.setCharacterEncoding(enCode);
        resp.setContentType("text/html;charset=utf-8");

        System.out.println("MyServlet 收到了 get 请求" + this+"\t" + req);
        // 接收客户端的数据
        // 获取url中的参数
        String aParams = req.getParameter("a");
        String bParams = req.getParameter("b");
        String[] cParams = req.getParameterValues("c");
        System.out.println("a的值：" + aParams + "  b的值："+ bParams +"  c的值：" + Arrays.toString(cParams));
        // 使用map接收所有参数
        Map<String, String[]> parameterMap = req.getParameterMap();
        for(Map.Entry<String, String[]> item : parameterMap.entrySet()){
            String key = item.getKey();
            String[] value = item.getValue();
            System.out.println("key:"+ key +"  value:"+ Arrays.toString(value));
        }

        // 部分api
        String contextPath = req.getContextPath();
        String servletPath = req.getServletPath();
        String requestURI = req.getRequestURI();
        String requestURL = req.getRequestURL().toString();
        String remoteAddr = req.getRemoteAddr();
        System.out.println("getContextPath "+contextPath);
        System.out.println("getServletPath "+servletPath);
        System.out.println("getRequestURI "+requestURI);
        System.out.println("getRequestURL "+requestURL);
        System.out.println("remoteAddr "+remoteAddr);
        System.out.println("getServerUrl:" + Utils.getServerUrl(req));
        LOG.info("getServerUrl:" + Utils.getServerUrl(req));

        // 获取请求头
        Enumeration<String> headerNames = req.getHeaderNames();
        while (headerNames.hasMoreElements()){
            String element = headerNames.nextElement();
            String headerValue = req.getHeader(element);
            System.out.println("请求头: " + element+":"+headerValue);
        }

        // 修改响应行
//        resp.setStatus(505);
        // 修改响应头
        resp.addHeader("abcd","1234");
        // 需要返回给浏览器的数据 资源等， 设置 HttpServletResponse的对象的属性
        PrintWriter out = resp.getWriter();
        out.println("这是我的第一个servlet");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("MyServlet 收到了 post 请求");
    }

    @Override
    public void destroy() {
        super.destroy();
        System.out.println("MyServlet destroy");

    }
}
